/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package quanlykho;

import com.sun.rowset.JdbcRowSetImpl;
import java.awt.Color;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author ADMIN
 */
public class UpdateHDNhap extends javax.swing.JFrame {

    /**
     * Creates new form UpdateHDNhap
     */
    JdbcRowSetImpl jdbcObj = null;
    DefaultTableModel model;
    Ketnoi a = new Ketnoi();
    Connection con;

    public UpdateHDNhap(String User) throws SQLException {
        initComponents();
        con = a.connect();
        PreparedStatement pst = con.prepareCall("Select * from dangnhap where [User] = ?");
        pst.setString(1, User);
        //System.out.println(User);
        ResultSet rs = pst.executeQuery();
        String mnv = null;
        while (rs.next()) {
            mnv = rs.getString(4);
        }
        //System.out.println(mnv);
        //MaNV.setText(mnv);
        PreparedStatement pst1 = con.prepareCall("Select * from nhanvien where MaNV = ?");
        pst1.setString(1, mnv);
        rs = pst1.executeQuery();
        String TenNV = null;
        while (rs.next()) {
            TenNV = rs.getString(2);
        }
        System.out.println(TenNV);
        TNV.setText(TenNV);

        con = a.connect();
        Statement st = null;
        try {
            st = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
                    ResultSet.CONCUR_UPDATABLE);
        } catch (SQLException ex) {
            Logger.getLogger(UpdateHDNhap.class.getName()).log(Level.SEVERE, null, ex);
        }
        ResultSet rs0 = st.executeQuery("SELECT * FROM phieunhap");
        jdbcObj = new JdbcRowSetImpl(rs0);
        jdbcObj.first();
        fillCurrent();


    }

    public void fillCurrent() {
        try {
            model = (DefaultTableModel) jt1.getModel();
            int num = jt1.getRowCount();
            for (int j = num - 1; j >= 0; j--) {
                model.removeRow(j);
            }

            if (jdbcObj != null) {
                MaPN.setText(jdbcObj.getString("MaPN"));
                MaNV.setText(jdbcObj.getString("Manv"));
                NgayNhap.setText(jdbcObj.getString("NgayNhap"));
                SoMH.setText(jdbcObj.getString("SoMH"));
                ThanhTien.setText(jdbcObj.getString("ThanhTien"));
                ThanhTien.setDisabledTextColor(Color.red);
                ThanhTien.setEnabled(false);
                //TenKH.setText(jdbcObj.getString("TenKH"));
                //DiaChiKH.setText(jdbcObj.getString("DiaChiKH"));
                //SoDTKH.setText(jdbcObj.getString("SoDTKH"));
                PreparedStatement pst1 = con.prepareCall("Select * from mathangnhap where Mapn = ?");
                String mapn = MaPN.getText();
                pst1.setString(1, mapn);
                ResultSet rs1 = pst1.executeQuery();

                while (rs1.next()) {
                    Vector b = new Vector();
                    b.add(rs1.getString(2));
                    String a = rs1.getString(2);
                    //System.out.println(a);
                    PreparedStatement pst2 = con.prepareCall("Select tenhh from hanghoa where Mahh = ?");
                    pst2.setString(1, a);
                    ResultSet rs2 = pst2.executeQuery();
                    while (rs2.next()) {
                        b.add(rs2.getString(1));
                    }
                    b.add(rs1.getInt(4));
                    b.add(rs1.getInt(3));
                    b.add(rs1.getInt(5));
                    model.addRow(b);
                }



            }

        } catch (SQLException ex) {
            Logger.getLogger(UpdateHDNhap.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        NgayNhap = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        TNV = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        ThanhTien = new javax.swing.JTextField();
        MaPN = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        SoMH = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jt1 = new javax.swing.JTable();
        MaNV = new javax.swing.JTextField();
        bPrevious = new javax.swing.JButton();
        bNext = new javax.swing.JButton();
        bFirst = new javax.swing.JButton();
        bLast = new javax.swing.JButton();
        bUpdate = new javax.swing.JButton();
        xoa = new javax.swing.JButton();
        xoa1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel5.setText("Ngày Nhập:");

        jLabel7.setText("Giá Trị Đơn Hàng:");

        TNV.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TNVActionPerformed(evt);
            }
        });

        jLabel6.setText("Sửa Phiếu Nhập");

        jLabel3.setText("Tên Nhân Viên:");

        jLabel2.setText("Mã Nhân Viên:");

        jLabel4.setText("Số Mặt Hàng:");

        jLabel1.setText("Mã Phiếu Nhập:");

        jt1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Mã Hàng Hóa", "Tên Hàng Hóa", "Số Lượng Nhập", "Giá Nhập", "Thành Tiền"
            }
        ));
        jScrollPane1.setViewportView(jt1);

        bPrevious.setText("<");
        bPrevious.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bPreviousActionPerformed(evt);
            }
        });

        bNext.setText(">");
        bNext.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bNextActionPerformed(evt);
            }
        });

        bFirst.setText("<<");
        bFirst.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bFirstActionPerformed(evt);
            }
        });

        bLast.setText(">>");
        bLast.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bLastActionPerformed(evt);
            }
        });

        bUpdate.setText("Update");
        bUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bUpdateActionPerformed(evt);
            }
        });

        xoa.setText("Xóa Hàng Hóa");
        xoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                xoaActionPerformed(evt);
            }
        });

        xoa1.setText("Xóa Phiếu Nhập");
        xoa1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                xoa1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(130, 130, 130)
                        .addComponent(bFirst)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bPrevious)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bNext)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bLast))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 126, Short.MAX_VALUE)
                                    .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jLabel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addGap(38, 38, 38)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(MaNV, javax.swing.GroupLayout.DEFAULT_SIZE, 161, Short.MAX_VALUE)
                                    .addComponent(MaPN)
                                    .addComponent(NgayNhap)
                                    .addComponent(SoMH)
                                    .addComponent(ThanhTien))
                                .addGap(45, 45, 45))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 127, Short.MAX_VALUE)
                                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(TNV, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(bUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(xoa, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(xoa1, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(47, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(TNV, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(MaPN, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(bUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(MaNV, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(NgayNhap, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(SoMH, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(ThanhTien, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(xoa, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(xoa1, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(58, 58, 58)))
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bFirst)
                    .addComponent(bPrevious)
                    .addComponent(bNext)
                    .addComponent(bLast))
                .addContainerGap(56, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void TNVActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TNVActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_TNVActionPerformed

    private void bPreviousActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bPreviousActionPerformed
        try {
            if (!jdbcObj.isFirst()) {
                jdbcObj.previous();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        fillCurrent();

    }//GEN-LAST:event_bPreviousActionPerformed

    private void bNextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bNextActionPerformed
        try {
            if (!jdbcObj.isLast()) {
                jdbcObj.next();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        fillCurrent();


    }//GEN-LAST:event_bNextActionPerformed

    private void bFirstActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bFirstActionPerformed
        try {
            jdbcObj.first();
        } catch (Exception e) {
            e.printStackTrace();
        }
        fillCurrent();


    }//GEN-LAST:event_bFirstActionPerformed

    private void bLastActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bLastActionPerformed
        try {
            jdbcObj.last();
        } catch (Exception e) {
            e.printStackTrace();
        }
        fillCurrent();


    }//GEN-LAST:event_bLastActionPerformed

    private void bUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bUpdateActionPerformed
//        try {
        int num = jt1.getRowCount();
        int d = 0;
        int s = 0;
        Ketnoi b = new Ketnoi();
        Connection con2;
        con2 = b.connect();
        for (int j = num - 1; j >= 0; j--) {
            jt1.setValueAt(Integer.parseInt(jt1.getValueAt(j, 2).toString()) * Integer.parseInt(jt1.getValueAt(j, 3).toString()), j, 4);
            System.out.println(Integer.parseInt(jt1.getValueAt(j, 4).toString()));
            d++;

            PreparedStatement pstmt = null;
            try {
                pstmt = con2.prepareStatement("update mathangnhap set gianhap=?, soluong=?, tong=? where mapn=? and mahh=?");
                pstmt.setInt(1, Integer.parseInt(jt1.getValueAt(j, 3).toString()));
                pstmt.setInt(2, Integer.parseInt(jt1.getValueAt(j, 2).toString()));
                pstmt.setInt(3, Integer.parseInt(jt1.getValueAt(j, 4).toString()));
                pstmt.setString(4, MaPN.getText());
                pstmt.setString(5, jt1.getValueAt(j, 0).toString());
                pstmt.executeUpdate();
            } catch (SQLException ex) {
                Logger.getLogger(UpdateHDNhap.class.getName()).log(Level.SEVERE, null, ex);
            }
            s += Integer.parseInt(jt1.getValueAt(j, 4).toString());
            ThanhTien.setText(String.valueOf(s));
        }
        PreparedStatement pstmt = null;
        try {
            pstmt = con2.prepareStatement("update phieunhap set manv=?, Ngaynhap=?,somh=?, thanhtien=? where mapn=?");
            pstmt.setString(1, MaNV.getText());
            pstmt.setString(2, NgayNhap.getText());
            pstmt.setInt(3, Integer.parseInt(SoMH.getText()));
            pstmt.setInt(4, Integer.parseInt(ThanhTien.getText()));
            pstmt.setString(5, MaPN.getText());
            pstmt.executeUpdate();
            JOptionPane.showMessageDialog(null, "Update successfully");
            con2.close();
            jdbcObj.refreshRow();
            fillCurrent();
        } catch (SQLException ex) {
            Logger.getLogger(UpdateHDNhap.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println(d);
        System.out.println(s);
//            con = a.connect();
//            PreparedStatement pstmt = null;
//            try {
//                pstmt = con.prepareStatement("update phieunhap set manv=?, Ngaynhap=?,somh=?, thanhtien=? where mapn=?");
//            } catch (SQLException ex) {
//                Logger.getLogger(UpdateHDNhap.class.getName()).log(Level.SEVERE, null, ex);
//            }
//
//            pstmt.setString(1, MaNV.getText());
//            pstmt.setString(2, NgayNhap.getText());
//            pstmt.setString(3, SoMH.getText());
//            pstmt.setString(4, ThanhTien.getText());
//            pstmt.setString(5, MaPN.getText());
//
//            pstmt.executeUpdate();
//            JOptionPane.showMessageDialog(null, "Update successfully");
//            fillCurrent();
//            
//        } catch (SQLException ex) {
//            Logger.getLogger(UpdateHDNhap.class.getName()).log(Level.SEVERE, null, ex);
//        }
    }//GEN-LAST:event_bUpdateActionPerformed

    private void xoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_xoaActionPerformed
        // TODO add your handling code here:
        int cot = 0;
        cot = jt1.getSelectedRow();
        if (cot < 0) {
            JOptionPane.showMessageDialog(this, "Hãy lựa chọn hàng cần xóa.");
        }
        if (cot >= 0) {
            try {
                String str = jt1.getValueAt(cot, 4).toString();
                int giatien = 0;
                giatien = Integer.parseInt(str);
                System.out.println(giatien);
                int tong = Integer.parseInt(ThanhTien.getText());
                tong = tong - giatien;
                ThanhTien.setText(String.valueOf(tong));
                Connection con;
                con = a.connect();
                PreparedStatement pstmt = null;
                try {
                    pstmt = con.prepareStatement("DELETE FROM mathangnhap WHERE mapn=? and mahh=?");
                    pstmt.setString(1, MaPN.getText());
                    System.out.println(MaPN.getText());                  
                    pstmt.setString(2, jt1.getValueAt(cot, 0).toString());
                    System.out.println(jt1.getValueAt(cot, 0).toString());
                    pstmt.executeUpdate();
                } catch (SQLException ex) {
                    Logger.getLogger(UpdateHDNhap.class.getName()).log(Level.SEVERE, null, ex);
                }
                
                JOptionPane.showMessageDialog(null, "Delete Hang Hoa Successfully!!!");
                model.removeRow(cot);

                int i = Integer.parseInt(SoMH.getText());
                i = i - 1;
                SoMH.setText(String.valueOf(i));
                try {
                    pstmt = con.prepareStatement("update phieunhap set manv=?, Ngaynhap=?,somh=?, thanhtien=? where mapn=?");
                } catch (SQLException ex) {
                    Logger.getLogger(UpdateHDNhap.class.getName()).log(Level.SEVERE, null, ex);
                }
                pstmt.setString(1, MaNV.getText());
                pstmt.setString(2, NgayNhap.getText());
                pstmt.setInt(3, Integer.parseInt(SoMH.getText()));
                pstmt.setInt(4, Integer.parseInt(ThanhTien.getText()));
                pstmt.setString(5, MaPN.getText());
                pstmt.executeUpdate();
                JOptionPane.showMessageDialog(null, "Update successfully");
               
                jdbcObj.refreshRow();
                fillCurrent();
            } catch (SQLException ex) {
                Logger.getLogger(UpdateHDNhap.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_xoaActionPerformed

    private void xoa1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_xoa1ActionPerformed
        // TODO add your handling code here:
        int num = jt1.getRowCount();
     
    
        Ketnoi b = new Ketnoi();
        Connection con2;
        con2 = b.connect();
        for (int j = num - 1; j >= 0; j--) {     
            PreparedStatement pstmt = null;
            try {
                pstmt = con2.prepareStatement("DELETE FROM mathangnhap WHERE mapn=? and mahh=?");
                pstmt.setString(1, MaPN.getText());
                pstmt.setString(2, jt1.getValueAt(j, 0).toString());
                pstmt.executeUpdate();
            } catch (SQLException ex) {
                Logger.getLogger(UpdateHDNhap.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        PreparedStatement pstmt = null;
        try {
            pstmt = con2.prepareStatement("DELETE FROM phieunhap WHERE mapn=?");   
            pstmt.setString(1, MaPN.getText());
            pstmt.executeUpdate();
            JOptionPane.showMessageDialog(null, "Update successfully");
            con2.close();
            jdbcObj.next();
            jdbcObj.refreshRow();
            fillCurrent();
        } catch (SQLException ex) {
            Logger.getLogger(UpdateHDNhap.class.getName()).log(Level.SEVERE, null, ex);
        }

        
    }//GEN-LAST:event_xoa1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(UpdateHDNhap.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(UpdateHDNhap.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(UpdateHDNhap.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(UpdateHDNhap.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new UpdateHDNhap("admin").setVisible(true);
                } catch (SQLException ex) {
                    Logger.getLogger(UpdateHDNhap.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField MaNV;
    private javax.swing.JTextField MaPN;
    private javax.swing.JTextField NgayNhap;
    private javax.swing.JTextField SoMH;
    private javax.swing.JTextField TNV;
    private javax.swing.JTextField ThanhTien;
    private javax.swing.JButton bFirst;
    private javax.swing.JButton bLast;
    private javax.swing.JButton bNext;
    private javax.swing.JButton bPrevious;
    private javax.swing.JButton bUpdate;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jt1;
    private javax.swing.JButton xoa;
    private javax.swing.JButton xoa1;
    // End of variables declaration//GEN-END:variables
}
